﻿using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using Kaleida.ServiceMonitor.Model;
using Kaleida.ServiceMonitor.Model.Statistics;

namespace Kaleida.ServiceMonitor.UI.NotificationPanels
{
    [PanelName("Statistics")]
    public partial class StatisticsNotificationPanel : UserControl, IModuleNotificationPanel
    {
        private Monitor monitor;
        private readonly List<OperationStatisticsControl> statsControls;
        private readonly Label lblMicroSummary;

        public StatisticsNotificationPanel()
        {
            InitializeComponent();
            statsControls = new List<OperationStatisticsControl>();

            lblMicroSummary = new Label { Visible = false, Dock = DockStyle.Fill };
            Controls.Add(lblMicroSummary);
        }

        public void Initialise(Monitor monitorInstance)
        {
            monitor = monitorInstance;
        }

        public void RefreshContent()
        {
            lock (tabStatistics)
            {
                var startTime = monitor.Statistics.Combined.StartTime;
                tbStartTime.Text = startTime == null ? "" : startTime.Value.ToString("dd-MMM-yyyy HH:mm:ss");

                if (StatsTabsNeedRefresh())
                {
                    tabStatistics.TabPages.Clear();
                    statsControls.Clear();
                    foreach (var stats in monitor.Statistics.AllValues)
                    {
                        AddStatisticsTabControl(stats);
                    }
                }
                else
                {
                    foreach (var statsControl in statsControls)
                    {
                        statsControl.UpdateUI();
                    }
                }

                lblMicroSummary.Text = PanelName + " [Double-click to enlarge]";
            }
        }

    	public string PanelName
    	{
			get { return "Statistics - " + monitor.CompiledModule.Name; }
    	}

    	public Control GetPanelControl()
        {
            return this;
        }

        private void AddStatisticsTabControl(ProcessingStatistics stats)
        {
            var statisticsControl = new OperationStatisticsControl(stats) { Dock = DockStyle.Fill };

            var tabPage = new TabPage(stats.Name);
            tabPage.Controls.Add(statisticsControl);

            statsControls.Add(statisticsControl);

            tabStatistics.TabPages.Add(tabPage);
        }

        private bool StatsTabsNeedRefresh()
        {
            if (monitor.Statistics.AllValues.Count() != statsControls.Count)
                return true;

            for (int i = 0; i < monitor.Statistics.AllValues.Count(); i++)
            {
                if (monitor.Statistics.AllValues[i] != statsControls[i].Statistics)
                    return true;
            }
            return false;
        }
        
        private void btnResetStats_Click(object sender, EventArgs e)
        {
            monitor.Statistics.ResetAll();
        }

        private void StatisticsNotificationPanel_Resize(object sender, EventArgs e)
        {
            bool isMini = Width < 320 || Height < 200;
            bool isMicro = Width < 50 || Height < 40;

            if (isMini)
            {
                tabStatistics.Dock = DockStyle.Fill;
            }
            else
            {
                tabStatistics.Dock = DockStyle.None;
                tabStatistics.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
                tabStatistics.Location = new Point(3, 32);
                tabStatistics.Size = new Size(Width - 6, Height - 26);
            }
            topPanel.Visible = !isMini && !isMicro;

            tabStatistics.Visible = !isMicro;
            lblMicroSummary.Visible = isMicro;
        }

    }
}
